Grokking Scalable Systems for Interviews
Rate this course
Ask Author
Contribute
Back to course home

4% completed

Vote For New Content
Introduction
Course Overview
Who This Course Is for
Caching
What Is Negative Caching and When Should You Cache 404 or Empty Results?
What Is the Difference Between Soft TTL and Hard TTL, and Why Does It Matter?
How Can a Bloom Filter Reduce Cache or Database Load?
What Is Distributed Locking for Cache Rebuilds, and How Does It Prevent Cache Stampedes?
Load Balancing
What Are Sticky Sessions (Session Affinity) and When Should I Avoid Them?
What Is the Difference between Liveness Checks and Readiness Checks in Load Balancers?
What is Global Server Load Balancing (GSLB) and How GeoDNS and Anycast Work?
What are the Main API Pagination Strategies (offset, cursor, keyset), and When Should I Use Each?
What are HTTP Conditional Requests (ETag, If‑None‑Match, Last‑Modified) and How They Reduce Load?
What Is the Difference between Rate Limiting and Throttling and Quotas?
What Are Idempotency Keys and How to Implement Them Safely for Payments
What are SQL Isolation Levels (Read Committed, Repeatable Read, Serializable), and What Anomalies Do They Prevent?
What Is a Write‑Ahead Log (WAL), and How Does It Ensure Durability?
What Is the Difference Between Synchronous and Asynchronous Replication, and When Should I Use Read Replicas?
What Is MVCC (Multi‑Version Concurrency Control), and How Does It Enable Concurrent Reads and Writes?
What Are Safe Patterns for Online Schema Changes vs. Offline Migrations?
What is Little’s Law and How to Use It for Quick Capacity Estimates in System Design
How Can I do Back-of-the-Envelope Sizing for QPS, Bandwidth, Storage, and Peak vs. Average Load?
What are Cold Starts and Warm Starts, and Why Do They Matter for Performance?
How Do I Choose a Good Shard Key and Avoid Hotspots (Bucketing and Key Randomization)?
What Are Range Sharding, Directory‑Based Sharding, and Geo‑Sharding, and What Are the Common Use Cases?
What Is the Difference Between Rendezvous Hashing and Consistent Hashing, and When Should I Use Each?
What Do At‑most‑once, At‑least‑once, And Exactly‑once Delivery Semantics Mean?
What Are Idempotent Producers And Consumers, And How Do De‑duplication Keys Work?
What Is Message Ordering, How Do Partition Keys Affect It, And When Can Ordering Break?
What Are Windowing And Watermarking In Streaming Systems, In Simple Terms?
What Is Quorum (N, R, W), And Why Does R + W > N Give Strongly Consistent Reads?
What Are Common Conflict Resolution Strategies (Last‑write‑wins, Vector Clocks, Logical Clocks)?
What Are Read Repair, Hinted Handoff, And Anti‑entropy (Merkle Trees) In Eventually Consistent Systems?
What Is The Difference Between SLI, SLO, And SLA, And Can You Give Simple Examples?
What Is Opentelemetry, And How Do Traces, Spans, Metrics, And Logs Fit Together?
What Is An Error Budget, And How Should It Guide Release Decisions?
What Are The Trade‑offs Between Server‑stored Sessions And JWTs for Authentication?
What Is The Difference Between TLS And MTLS, And When Is MTLS Appropriate For Service‑to‑service Auth?
What Is Secrets Management, And How Do Environment Variables, KMS, And Vault Compare?
What Is A Replay Attack, And How Is It Different from Idempotency Issues?
What Are The Differences Between TCP, UDP, And QUIC, And When Should I Use Each?
What Is The Difference Between A Forward Proxy, A Reverse Proxy, And NAT?
What Changed From HTTP/1.1 To HTTP/2 To HTTP/3, And What Are Head‑of‑line Blocking And Multiplexing?
What Is the Difference Between Content‑Addressable Storage and Location‑Based Addressing?
What Are Checksums and Etags, and How Do MD5/SHA Compare to CRC for Integrity?
What Are Hot, Warm, Cold, and Archive Storage Tiers, and When Should I Use Each?
What Is the Outbox Pattern, and How Does Change Data Capture (CDC) Work at a High Level?
What Is CQRS (Command Query Responsibility Segregation), and When Should I Consider It?
What Are the Differences Between a Circuit Breaker, Retry With Backoff, and Rate Limiting?
What Are RPO and RTO, and How Do They Differ in Disaster Recovery Planning?
What Is the Difference Between Active‑Active and Active‑Passive Architectures?
What Is Graceful Degradation, and How Do Feature Flags Help Availability?
What Are the Differences Between UUID, ULID, KSUID, and Snowflake IDs, and How Do I Choose?
How Do Snowflake‑Style IDs Work (Timestamp, Worker, Sequence), and What Problems Do They Solve?
What Is the Difference Between Wall‑Clock Time and Monotonic Time, and Why Is Clock Skew Hard?
Final Words
Key Takeaways
What To Learn Next
What Are Sticky Sessions (Session Affinity) and When Should I Avoid Them?

Sticky sessions, also known as session affinity, is a load balancing technique that ensures all requests from a specific user are consistently routed to the same backend server throughout that user's session.

In simpler terms, the load balancer “sticks” a user to one server so that their session data (like login status or shopping cart) remains intact on that server.

Understanding Sticky Sessions (Session Affinity)

In a typical web application deployed on multiple servers, a load balancer distributes incoming requests across the servers for efficiency.

However, HTTP is a stateless protocol, which means each request is independent and does not remember previous interactions. This can lead to problems if user-specific data is stored in a single server’s memory.

For example, if you’re shopping online and adding items to your cart, you might suddenly find yourself logged out or your cart emptied if a new request gets routed to a different server that doesn’t have your session information.

Without Sticky Sessions
Without Sticky Sessions

Sticky sessions solve this problem by binding a user’s session to one server, ensuring continuity of data and experience.

To achieve session affinity, the load balancer needs a way to identify and remember which server a user is tied to.

Typically, this is done by assigning an identifier to the user, often via a cookie in the user’s browser or by tracking the client’s IP address.

On the user’s first request, the load balancer might set a special session cookie that encodes which server handled the request.

Thereafter, the load balancer reads this cookie on each request and routes the user to the same server that served them initially. (IP-based sticky sessions are another method, but are less reliable if multiple users share an IP or if a single user’s IP changes.)

With Sticky Sessions
With Sticky Sessions

This way, all session data (like your login state or cart contents) stays on one server, and the application doesn’t need to constantly share session information between servers.

An easy way to understand sticky sessions is by analogy: it’s like having a favorite waiter at a restaurant who always serves you and remembers your order.

Just as that waiter can provide personalized, consistent service because they know you, a specific server can handle all of a user’s requests and “remember” their session data.

This improves user experience by avoiding scenarios where information might get lost due to bouncing between different servers.

Benefits of Sticky Sessions

Sticky sessions exist to address real needs in session management.

Some key benefits include:

  • Consistent Session Data / Simplicity: All of a user’s session data is kept on a single server, simplifying session management by removing the need to replicate or share session state across multiple servers. This consistency means a user’s experience (e.g. staying logged in, items remaining in cart) is uninterrupted because the server “remembers” their data.

  • Improved Performance with Caching: By sticking users to one server, you can take advantage of that server’s in-memory caches. For example, the server might cache user information or frequently accessed data in RAM; with affinity, subsequent requests from the same user hit the warm cache instead of starting fresh on another server. This avoids the overhead of transferring session data between servers and can lead to faster response times for the user.

  • Easy Implementation (No Code Changes): Enabling sticky sessions is usually as simple as a configuration change on the load balancer and requires no changes to application code. Most modern load balancers (NGINX, HAProxy, cloud load balancers, etc.) support session affinity out of the box. This makes it an attractive quick solution for session management, especially in legacy applications. You don’t have to redesign how sessions are stored, since the load balancer takes care of keeping a user’s traffic on the same server.

Drawbacks of Sticky Sessions

Despite their benefits, sticky sessions come with notable drawbacks that can impact scalability and reliability:

  • Scalability Limitations (Uneven Load): Session affinity can lead to uneven load distribution across servers. If many users happen to get “stuck” to one particular server, that machine can become overloaded while others remain underutilized. This imbalance means your application may not fully benefit from having multiple servers, defeating the purpose of load balancing. It also makes scaling out harder. For instance, when you add a new server to the pool, it won’t immediately receive traffic if existing users are all stuck to their current servers, slowing down the effectiveness of scaling.

  • Single Point of Failure (Session Loss on Crash): With sticky sessions, crucial session data lives only in one server’s memory by default. If that server goes down or restarts, all users tied to it will lose their sessions (e.g. they could be logged out or lose unsaved data). In a high-availability environment, this is problematic because it creates a single point of failure for those users’ experience. Without additional session replication, session affinity means a server crash can directly translate to a poor user experience as active sessions vanish.

  • Challenges in Dynamic or Distributed Environments: Modern cloud deployments often add or remove servers (or containers) on the fly to meet demand. Sticky sessions complicate this. As servers are dynamically added or removed, existing session affinities can become invalid or hard to maintain. For example, if a server instance is terminated (for auto-scaling or maintenance), all sessions on it are disrupted. If new instances start, they might stay mostly idle until new users connect, because existing users are still pinned to older instances. Managing session affinity in container orchestration (like Kubernetes) or multi-datacenter setups becomes complex, potentially requiring “stateful” load balancer configurations that counteract the fluid, stateless nature of cloud scaling.

  • Maintenance and Long Sessions: Because users are tied to specific servers, performing maintenance or updates on a server is harder. You might have to drain or wait for all sessions on that server to finish or expire. Long-lived user sessions can tie up resources on one machine for extended periods, making it difficult to take that server out of rotation for updates without disrupting those users. This rigidity is at odds with the flexibility needed for seamless deployments and upgrades.

  • Limited Fault Tolerance for Heavy Users: In some cases, a single user’s session might generate a very high load (imagine a user performing many complex operations). With sticky sessions, all of that load hits one server, potentially slowing down that user’s experience if the server maxes out, whereas a truly stateless setup might allow spreading requests across multiple servers. In general, if one user’s traffic is extreme enough to matter, it highlights that sticky sessions prevent distributing even a single user’s workload across the cluster.

When Should You Avoid Sticky Sessions?

Given the above drawbacks, when is it better not to use sticky sessions?

In modern web architecture, the default approach is to design systems that don’t require session affinity unless absolutely necessary. You should consider avoiding sticky sessions in the following scenarios:

  • High Availability Requirements: If your application is mission-critical and cannot afford to lose user session data due to a server crash, avoid sticky sessions. Relying on affinity means a single server’s failure can wipe out all sessions on it, so applications that need robust failover and zero session loss should use a different approach (like external session storage) to survive crashes.

  • Horizontal Scalability and Auto-Scaling: If you need to scale your application seamlessly or handle sudden traffic spikes, sticky sessions can hinder you. Avoid them when you want load to be evenly distributed at all times. In auto-scaling environments (Kubernetes pods, cloud auto-scaling groups), new server instances should share the load immediately, and old ones should be removable without incident. Session affinity works against this by anchoring users to specific servers, so it’s best avoided in elastic cloud scenarios.

  • Globally Distributed or Microservices Architectures: For applications distributed across multiple regions or composed of many microservices, managing state on individual servers becomes complex. If you have a global user base that might be served from different data centers, or if your architecture involves calling multiple backend services, sticking a user to one server can introduce latency or imbalance. It’s usually better to avoid sticky sessions and use a strategy that allows any server or service instance to serve the user, which simplifies traffic management in distributed systems.

  • Modern Stateless Application Design: If you have the option to redesign or configure your application to be stateless (not relying on in-memory server sessions), do so instead of using sticky sessions. Following twelve-factor app principles, storing session state externally (or not at all) is a best practice for cloud-native apps. In cases where you can use a token-based authentication (JWT) or a shared database/cache for sessions, you should avoid sticky sessions because these alternatives provide better scalability and resilience.

In summary, avoid session affinity unless you have a specific requirement for it in a stateful legacy application.

Many organizations initially use sticky sessions for convenience, but later discover it’s better to remove that dependency as they scale.

Alternatives to Sticky Sessions

If you decide to avoid sticky sessions, how do you maintain user session state across a cluster of servers?

Here are some alternative approaches that address the same problem in a more scalable way:

  • Centralized Session Store: Instead of keeping session data in each server’s memory, store it in a centralized datastore that all servers can access. Popular choices are in-memory data grids or caches like Redis or Memcached, or dedicated session databases. With this approach, it doesn’t matter which server handles a request. It can always retrieve the user’s session data from the central store. This enables true load balancing and failover, since any server can pick up where another left off.

  • Stateless Tokens (JWT): Use a stateless authentication mechanism, such as JSON Web Tokens, where the session information (like user ID and claims) is encoded into a token that the client carries. The server doesn’t need to remember anything about the user between requests, because each request includes the token that proves the user’s identity and session data. This eliminates the need for server-side sessions altogether, meaning no affinity is required. Any server can serve any request as long as it can validate the token.

  • Database-Backed Sessions: Store session state in a shared database. For example, web frameworks often allow using an SQL or NoSQL database for session storage. This way, even if a user’s subsequent request goes to a different server, that server can load the session data from the database. This method introduces a bit of read/write latency, but ensures sessions persist beyond a single server’s memory. It also provides durability; if a server crashes, the session isn’t lost since it lives in the database.

Each of these alternatives allows you to maintain user state without pinning users to a single server, thereby improving the overall scalability and fault tolerance of your application.

They require more initial setup than flipping on sticky sessions, but they are considered best practice for large-scale systems.

What Is Distributed Locking for Cache Rebuilds, and How Does It Prevent Cache Stampedes?
What Is the Difference between Liveness Checks and Readiness Checks in Load Balancers?
Mark as Completed

On this page

Understanding Sticky Sessions (Session Affinity)

Benefits of Sticky Sessions

Drawbacks of Sticky Sessions

When Should You Avoid Sticky Sessions?

Alternatives to Sticky Sessions